package cn.topcodes.tcsf.admin.web.controller.impl;

import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.service.IntegrityCheckRule;
import cn.topcodes.tcsf.admin.service.IntegrityCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Unicorn on 2017/12/29.
 */
@Controller
@RequestMapping("/admin/integrity-check")
public class IntegrityCheckAdminController {

    @Autowired
    private IntegrityCheckService integrityCheckService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(Model model) {
        List<IntegrityCheckRule> rules = integrityCheckService.getRules();
        model.addAttribute("rules", rules);
        return "admin/integrity-check";
    }

    /**
     * 数据完整性检查
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "check", method = RequestMethod.GET)
    public ApiResponseEntity check() {
        integrityCheckService.startCheck();
        return ApiResponseEntity.buildOK();
    }

    /**
     * 数据完整性检查
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "check-rule", method = RequestMethod.GET)
    public ApiResponseEntity checkRule(
            @RequestParam String id
    ) {
        integrityCheckService.checkRule(id);
        return ApiResponseEntity.buildOK();
    }
}
